Page({
  data: {
    title: null,
    historyList: []
  },

  onLoad: function (event) {
    this.setData({
      title: event.historyName
    })
    if (this.data.title == '调度历史') {
      this.getDispatchList()
    } else if (this.data.title == '请求历史') {
      this.getRequestList()
    } else {
      this.getRewardsList()
    }
  },

  onshow: function (event) {
    if (this.data.title == '调度历史') {
      this.getDispatchList()
    } else if (this.data.title == '请求历史') {
      this.getRequestList()
    } else {
      this.getRewardsList()
    }
  },

  goScoreRule() {
    wx.showModal({
      title: '提示',
      content: '提交请求或进行单车调度均可获得一定量的积分，具体获取积分表如下：\n乱停乱放：30积分；\n单车淤积：30积分；\n申请调度：30积分；\n调度单车：50积分',
      showCancel: false
    })
  },

  goRewardsApplication() {
    wx.showModal({
      title: '提示',
      content: '兑换的奖券会定期同步至对应单车公司的APP卡包中，用于单车的优惠骑行！',
      showCancel: false
    })
  },

  goExchangeRewards(event) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定要兑换吗？',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          that.exchangeRewards(event.currentTarget.dataset.rewardsId)
        }
      }
    })
  },

  exchangeRewards(rewardsId) {
    wx.showLoading({
      title: '正在加载...'
    });
    wx.request({
      url: getApp().globalData.commonIP + '/rewards/exchangeRewardsByNickName',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      data: {
        nickName: wx.getStorageSync('nickName'),
        rewardsId: rewardsId
      },
      success: res => {
        wx.hideLoading();
        if (res.data.msg == "token过期，请重新登录" || res.data.msg == "token失效，请重新登录" || res.data.msg == "无效签名，请重新登录" || res.data.msg == "token算法不一致，请重新登录") {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            complete: (res) => {
              if (res.cancel) {

              }

              if (res.confirm) {
                // 重置全局变量和去除缓存
                getApp().globalData.wxusername = ''
                getApp().globalData.wxuseravatar = ''
                wx.removeStorageSync('nickName')
                wx.removeStorageSync('avatarUrl')
                wx.removeStorageSync('token')

                wx.switchTab({
                  url: '/pages/index/index'
                })
              }
            }
          })
          return
        }
        wx.showModal({
          title: '提示',
          content: res.data.msg,
          showCancel: false
        })
      }
    })
  },

  getDispatchList() {
    var that = this
    wx.request({
      url: getApp().globalData.commonIP + '/bikeDispatch/getDispatchListByNickName',
      method: 'GET',
      header: {
        'content-type': 'application',
        'token': wx.getStorageSync('token')
      },
      data: {
        nickName: wx.getStorageSync('nickName')
      },
      success: res => {
        if (res.data.msg == "token过期，请重新登录" || res.data.msg == "token失效，请重新登录" || res.data.msg == "无效签名，请重新登录" || res.data.msg == "token算法不一致，请重新登录") {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            complete: (res) => {
              if (res.cancel) {

              }

              if (res.confirm) {
                // 重置全局变量和去除缓存
                getApp().globalData.wxusername = ''
                getApp().globalData.wxuseravatar = ''
                wx.removeStorageSync('nickName')
                wx.removeStorageSync('avatarUrl')
                wx.removeStorageSync('token')

                wx.switchTab({
                  url: '/pages/index/index'
                })
              }
            }
          })
          return
        }
        if (res.data.msg == '操作成功') {
          that.setData({
            historyList: res.data.data
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  },

  getRequestList() {
    var that = this
    wx.request({
      url: getApp().globalData.commonIP + '/bikeRequest/getRequestsByNickName',
      method: 'GET',
      header: {
        'content-type': 'application',
        'token': wx.getStorageSync('token')
      },
      data: {
        nickName: wx.getStorageSync('nickName')
      },
      success: res => {
        if (res.data.msg == "token过期，请重新登录" || res.data.msg == "token失效，请重新登录" || res.data.msg == "无效签名，请重新登录" || res.data.msg == "token算法不一致，请重新登录") {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            complete: (res) => {
              if (res.cancel) {

              }

              if (res.confirm) {
                // 重置全局变量和去除缓存
                getApp().globalData.wxusername = ''
                getApp().globalData.wxuseravatar = ''
                wx.removeStorageSync('nickName')
                wx.removeStorageSync('avatarUrl')
                wx.removeStorageSync('token')

                wx.switchTab({
                  url: '/pages/index/index'
                })
              }
            }
          })
          return
        }
        if (res.data.msg == '操作成功') {
          that.setData({
            historyList: res.data.data
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  },

  getRewardsList() {
    var that = this
    wx.request({
      url: getApp().globalData.commonIP + '/rewards/getRewardsForHistory',
      method: 'GET',
      header: {
        'content-type': 'application',
        'token': wx.getStorageSync('token')
      },
      success: res => {
        if (res.data.msg == "token过期，请重新登录" || res.data.msg == "token失效，请重新登录" || res.data.msg == "无效签名，请重新登录" || res.data.msg == "token算法不一致，请重新登录") {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            complete: (res) => {
              if (res.cancel) {

              }

              if (res.confirm) {
                // 重置全局变量和去除缓存
                getApp().globalData.wxusername = ''
                getApp().globalData.wxuseravatar = ''
                wx.removeStorageSync('nickName')
                wx.removeStorageSync('avatarUrl')
                wx.removeStorageSync('token')

                wx.switchTab({
                  url: '/pages/index/index'
                })
              }
            }
          })
          return
        }
        if (res.data.msg == '操作成功') {
          that.setData({
            historyList: res.data.data
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  }

})